﻿
Partial Class WebForm_ManageCatagory_CatagoryItem
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.Page.IsPostBack Then
            Me.BindDDLDivision()
            Me.BindDDLSignature()
            Me.BindDDLCatagoryLevel()
            Me.BindGV()
        End If
    End Sub

    Private Sub BindDDLDivision()
        Dim ddlControl As DropDownList = Me.ddlDivision
        Dim dvManager As New DivisionManager

        ddlControl.DataTextField = "Division"
        ddlControl.DataValueField = "Division"
        ddlControl.DataSource = dvManager.GetAllDivision()
        ddlControl.DataBind()
    End Sub

    Private Sub BindDDLSignature()
        Dim ddlControl As DropDownList = Me.ddlSignature

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0
        sql.AppendFormat(" select b.Description,Code from DivisionBrandMappingView a ")
        sql.AppendFormat(" left join ProductHierarchyOriginal b on a.Brand=b.Code and b.LevelName='Signature' ")
        sql.AppendFormat(" where a.Division='{0}' ", Me.ddlDivision.SelectedValue)
        sql.AppendFormat(" Order by b.Description ")

        ddlControl.DataTextField = "Description"
        ddlControl.DataValueField = "Code"
        ddlControl.DataSource = dat.GetData(sql.ToString())
        ddlControl.DataBind()
        ddlControl.Items.Insert(0, MyObj.GetDDLSelectAllItem())
    End Sub

    Private Sub BindDDLCatagoryLevel()
        Dim ddlControl As DropDownList = Me.ddlCatagoryLevel
        ddlControl.Items.Clear()
        ddlControl.Items.Add(New ListItem("Second", 2))
        ddlControl.Items.Add(New ListItem("Third", 3))
    End Sub

    Protected Sub ddlDivision_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDivision.SelectedIndexChanged
        Me.BindDDLSignature()
        Me.BindGV()
    End Sub

    Protected Sub ddlSignature_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSignature.SelectedIndexChanged
        Me.BindGV()
    End Sub

    Protected Sub ddlCatagoryLevel_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCatagoryLevel.SelectedIndexChanged
        Me.BindGV()
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then

        End If
    End Sub

    Protected Sub gv_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv.RowEditing
        Dim key As DataKey = gv.DataKeys(e.NewEditIndex)
        Me.HeaderID = key("HeaderID").ToString()
        Me.HeaderCode = key("HeaderCode").ToString()
        Session("Description" & Me.HeaderID) = key("NewDescription").ToString()
        Dim open As String = "window.open('" + Me.ResolveUrl(String.Format("~/Webform/ManageCatagory/OpenCatagoryMapping.aspx?HeaderID={0}&HeaderCode={1}&CatagoryLevel={2}", Me.HeaderID, Me.HeaderCode, Me.CatagoryLevel)) + "','newwindow','height=575px,toolbar=0,titlebar=0,resizable,scrollbars=Yes');"
        ScriptManager.RegisterClientScriptBlock(TryCast(HttpContext.Current.Handler, Page), GetType(String), "onclick", open.ToString, True)

    End Sub

    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        sql.AppendFormat(" Select a.Division,a.HeaderID,c.Description+'_'+a.NewDescription as NewDescription ")
        sql.AppendFormat(" ,c.Code as HeaderCode ")
        sql.AppendFormat(" from CodeCatagoryHeader a ")
        sql.AppendFormat(" left join DivisionBrandMappingView b on a.Division=b.Division ")
        sql.AppendFormat(" left join ProductHierarchyOriginal c on b.Brand=c.Code and c.LevelName='Signature' ")
        sql.AppendFormat(" Where 1=1 ")
        sql.AppendFormat(" And CatagoryLevel='{0}' ", Me.CatagoryLevel)
        sql.AppendFormat(" And a.Division = '{0}' ", Me.ddlDivision.SelectedValue)
        If Me.ddlSignature.SelectedValue <> "" Then
            sql.AppendFormat(" And c.Code = '{0}' ", Me.ddlSignature.SelectedValue)
        End If
        sql.AppendFormat(" order by c.Description+'_'+a.NewDescription ")

        Me.gv.DataSource = dat.GetData(sql)
        Me.gv.DataBind()

    End Sub

#Region "Property"
    Public Property HeaderID() As String
        Get
            Return ViewState("HeaderID")
        End Get
        Set(ByVal value As String)
            ViewState("HeaderID") = value
        End Set
    End Property

    Public Property HeaderCode() As String
        Get
            Return ViewState("HeaderCode")
        End Get
        Set(ByVal value As String)
            ViewState("HeaderCode") = value
        End Set
    End Property

    Public ReadOnly Property CatagoryLevel() As String
        Get
            Return Me.ddlCatagoryLevel.SelectedValue
        End Get
    End Property
#End Region

End Class
